// pages/rankPage/rankPage.js
const dataType = require('../../data/data')
const db = wx.cloud.database()
//便便时间榜，便便次数榜
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabList: [
      { title: "便便时间榜", index: "0", },
      { title: "便便次数榜", index: "1", }
    ],
    babaTimesSort: [],
    babaMinutesSort: [],
    tabsId: 0, //默认选型为装备
  
  },

  slideOn(e) {
    // 拿到当前索引并动态改变
    this.setData({
      tabsId: e.detail.current
    })
  },

  //点击tab时触发
  tabsOn(e) {
    this.setData({
      //拿到当前索引并动态改变
      tabsId: e.currentTarget.dataset.id
    })
  },

   minutesSort(getUserInfo,res) {
    const userMinutes =res.data.map(item => ({
      useerId: item.userId,
      userAvatarUrl: item.avatarUrl||'/images/default.jpg',
      userName: item.userName,
      babaMinutes: (item.babaMinutes / 60).toFixed(2),
      qianMing:item.qianMing
    }))
    userMinutes.sort((a, b) => b.babaMinutes - a.babaMinutes)
    userMinutes.forEach((item, idx) => {
      item.index = idx + 1
    })
    this.setData({
      babaMinutesSort: userMinutes
    })
  },

   timesSort(resDataBaseInfo,res) {
    console.log(res)
    const userTimes = res.data.map(item => ({
      userId: item.userId,
      userAvatarUrl: item.avatarUrl||'/images/default.jpg',
      userName: item.userName,
      babaTimes: item.babaTimes,
      qianMing:item.qianMing
    }))
    userTimes.sort((a, b) => b.babaTimes - a.babaTimes)
    userTimes.forEach(item=>{
        console.log(item.userAvatarUrl)
    })
    userTimes.forEach((item, idx) => {
      item.index = idx + 1
    })
    this.setData({
      babaTimesSort: userTimes
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
   
  },
  onUnload() {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    const res = await db.collection('user').get()
    const getUserInfo = wx.getStorageSync('userInfoResult')
    this.minutesSort(getUserInfo,res)
    this.timesSort(getUserInfo,res)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})